﻿using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Demos.DesignPatterns._01_Singleton
{
    /// <summary>
    /// 购物车服务
    /// </summary>
    public class ShoppingCartService
    {
        private static ShoppingCartService _instance;
        private static readonly object _lock = new object();

        // 使用线程安全的集合存储购物车项
        private readonly ConcurrentDictionary<int, CartItem> _cartItems;

        private ShoppingCartService()
        {
            _cartItems = new ConcurrentDictionary<int, CartItem>();
            Console.WriteLine("购物车服务初始化完成");
        }

        /// <summary>
        /// 获取购物车实例对象
        /// </summary>
        public static ShoppingCartService Instance
        {
            get
            {
                if (_instance == null)
                {
                    lock (_lock)
                    {
                        if (_instance == null)
                        {
                            _instance = new ShoppingCartService();
                        }
                    }
                }
                return _instance;
            }
        }

        /// <summary>
        /// 添加商品到购物车
        /// </summary>
        /// <param name="item"></param>
        public void AddToCart(CartItem item)
        {
            _cartItems.AddOrUpdate(item.ProductId,
                item,
                (id, existingItem) =>
                {
                    existingItem.Quantity += item.Quantity;
                    return existingItem;
                });
            Console.WriteLine($"商品 {item.ProductName} 已添加到购物车");
        }


        /// <summary>
        /// 从购物车移除商品
        /// </summary>
        /// <param name="productId"></param>
        /// <returns></returns>
        public bool RemoveFromCart(int productId)
        {
            bool removed = _cartItems.TryRemove(productId, out _);
            if (removed) Console.WriteLine($"商品ID {productId} 已从购物车移除");
            return removed;
        }

        /// <summary>
        /// 获取购物车总金额
        /// </summary>
        /// <returns></returns>
        public decimal GetTotalAmount()
        {
            decimal total = 0;
            foreach (var item in _cartItems.Values)
            {
                total += item.Price * item.Quantity;
            }
            return total;
        }

        /// <summary>
        /// 显示购物车内容
        /// </summary>
        public void DisplayCart()
        {
            Console.WriteLine("\n当前购物车内容:");
            foreach (var item in _cartItems.Values)
            {
                Console.WriteLine($"{item.ProductName} x{item.Quantity} - 单价: {item.Price:C}");
            }
            Console.WriteLine($"总计: {GetTotalAmount():C}");
        }

        /// <summary>
        /// 清空购物车
        /// </summary>
        public void ClearCart()
        {
            _cartItems.Clear();
            Console.WriteLine("购物车已清空");
        }
    }
}
